<?php

namespace app\shop\model\user;

use app\common\model\user\UserDelivery as UserDeliveryModel;
use think\Exception;

/**
 * 用户模型类
 */
class UserDelivery extends UserDeliveryModel
{
    /**
     * 隐藏字段
     */
    public static function listData($where, $params, $with = [])
    {
        $where = UserDelivery::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        $model = $model->order(UserDelivery::baseOrder());
        $model = UserDelivery::assWhere($model, $where);
        $list = $model->paginate($params);
        return $list;
    }

    public static function createData($where)
    {
        $model = new static();
        $where['app_id'] = self::$app_id;
        $where['code'] = randomString(8);
        $userModel = new User();
        $user = $userModel->where(['mobile' => $where['phone']])->count();
        if ($user <= 0) {
            throw new Exception("用户不存在");
        }
        $phoneCount = $model->where(['phone' => $where['phone']])->count();
        if ($phoneCount > 0) {
            throw new Exception("该电话已注册");
        }
        $data = $model->save($where);
        return $data;
    }

    public static function one($where)
    {
        $model = new static();
        return $model->findOrFail($where);
    }

    public static function deleteData($where)
    {
        $model = new static();
        return $model->where(['id' => $where['id']])->delete();
    }

}
